from copy import deepcopy

from utils.log import Log
from utils.bit_op import BitOp


class Utils(Log):
    def __init__(self, arr):
        super(Utils, self).__init__()
        self.arr = arr
        self.len = len(arr)

    def swap(self, left, right):
        if left != right:
            self.arr[left], self.arr[right] = self.arr[right], self.arr[left]


class TwoNumber(Log):
    def __init__(self):
        super(TwoNumber, self).__init__()

    def gcd(self, a, b):
        # 最大公约数 (Greatest Common Divisor)
        if b == 0:
            return a
        return self.gcd(b, a % b)

    def lcm(self, a, b):
        # 最小公倍数 (Least Common Multiple)
        return int(a / self.gcd(a, b) * b)


class BinSearch(Log):
    def __init__(self, arr, reverse=False):
        super(BinSearch, self).__init__()
        self.arr = sorted(list(set(deepcopy(arr))), reverse=reverse)

    def search(self, node, left=None, right=None):
        left = 0 if left is None else left
        right = self.len - 1 if right is None else right
        if left == right:
            if self.arr[left] != node:
                return None
            else:
                return left
        middle = BitOp.middle(left, right)
        if self.arr[middle] == node:
            return middle
        if self.arr[middle] > node:
            return self.search(node, left, middle - 1) if middle - 1 >= left else None
        if self.arr[middle] < node:
            return self.search(node, middle + 1, right) if middle + 1 <= right else None


if __name__ == '__main__':
    print(TwoNumber().gcd(30, 50))
    print(TwoNumber().lcm(6, 8))
